package com.wordplat.quickstart.xutils.common;

import java.lang.reflect.Type;

/**
 * Created by wyouflf on 15/6/5.
 * 通用回调接口
 *
 * @since 2021-05-09
 */
public interface Callback {

    interface CommonCallback<ResultType> extends Callback {
        /**
         * onSuccess
         *
         * @param result
         */
        void onSuccess(ResultType result);

        /**
         * onError
         *
         * @param ex
         * @param isOnCallback
         */
        void onError(Throwable ex, boolean isOnCallback);

        /**
         * onCancelled
         *
         * @param cex
         */
        void onCancelled(CancelledException cex);

        /**
         * onFinished
         */
        void onFinished();
    }

    interface TypedCallback<ResultType> extends CommonCallback<ResultType> {
        /**
         * getLoadType
         *
         * @return Type
         */
        Type getLoadType();
    }

    interface CacheCallback<ResultType> extends CommonCallback<ResultType> {
        /**
         * onCache
         *
         * @param result
         * @return boolean
         */
        boolean onCache(ResultType result);
    }

    interface ProxyCacheCallback<ResultType> extends CacheCallback<ResultType> {
        /**
         * onlyCache
         *
         * @return boolean
         */
        boolean onlyCache();
    }

    interface PrepareCallback<PrepareType, ResultType> extends CommonCallback<ResultType> {
        /**
         * prepare
         *
         * @param rawData
         * @return ResultType
         * @throws Throwable
         */
        ResultType prepare(PrepareType rawData) throws Throwable;
    }

    interface ProgressCallback<ResultType> extends CommonCallback<ResultType> {
        /**
         * onWaiting
         */
        void onWaiting();

        /**
         * onStarted
         */
        void onStarted();

        /**
         * onLoading
         *
         * @param total
         * @param current
         * @param isDownloading
         */
        void onLoading(long total, long current, boolean isDownloading);
    }

    interface GroupCallback<ItemType> extends Callback {
        /**
         * onSuccess
         *
         * @param item
         */
        void onSuccess(ItemType item);

        /**
         * onError
         *
         * @param item
         * @param ex
         * @param isOnCallback
         */
        void onError(ItemType item, Throwable ex, boolean isOnCallback);

        /**
         * onCancelled
         *
         * @param item
         * @param cex
         */
        void onCancelled(ItemType item, CancelledException cex);

        /**
         * onFinished
         *
         * @param item
         */
        void onFinished(ItemType item);

        /**
         * onAllFinished
         */
        void onAllFinished();
    }

    interface Callable<ResultType> {
        /**
         * call
         *
         * @param result
         */
        void call(ResultType result);
    }

    /**
     * Cancelable
     *
     * @since 2021-05-09
     */
    interface Cancelable {
        /**
         * cancel
         */
        void cancel();

        /**
         * isCancelled
         *
         * @return boolean
         */
        boolean isCancelled();
    }

    /**
     * CancelledException
     */
    class CancelledException extends RuntimeException {
        /**
         * CancelledException
         *
         * @param detailMessage
         */
        public CancelledException(String detailMessage) {
            super(detailMessage);
        }
    }
}
